// Copyright Leonid Shlyapnikov 2010
// Distributed under the GNU Lesser General Public License http://www.gnu.org/licenses/lgpl.html
//    author: Leonid Shlyapnikov, created on: Mar 17, 2010

#include "./Tag.h"

#include <boost/lexical_cast.hpp>
#include <boost/exception/all.hpp>
#include <stdexcept>
#include <sstream>

#include "./log.h"

namespace fixparser {

size_t get_number_of_decimal_digits(int tagId) {
    if (1 <= tagId && tagId <= 9) {
        return 1;
    } else if (10 <= tagId && tagId <= 99) {
        return 2;
    } else if (100 <= tagId && tagId <= 999) {
        return 3;
    } else if (1000 <= tagId && tagId <= 9999) {
        return 4;
    } else {
        std::stringstream error;
        error << "Illegal tagId: " << tagId << ", FIX tag must be in the interval: (0, 9999]";
        BOOST_THROW_EXCEPTION(std::invalid_argument(error.str()));
    }
}

TagConverter::TagConverter() {
    LOG_DEBUG("initializing TagConverter");
    for (int indx = 0; indx < MAX_TAG_ID; indx++) {
        int tagId = indx + 1;
        tagIdToStringMap_[indx] = boost::lexical_cast<std::string>(tagId);
    }
}

const std::string&
TagConverter::tagIdToStringInternal(int tagId) const {
    if (tagId < MIN_TAG_ID || tagId > MAX_TAG_ID) {
        std::stringstream error;
        error << "Illegal tagId: " << tagId << ", FIX tag must be in the interval: [1, 9999]";
        BOOST_THROW_EXCEPTION(std::invalid_argument(error.str()));
    }

    return tagIdToStringMap_[tagId - 1];
}

}
